<?php

namespace Home\Controller;

class ShopIndexController extends CommonController
{
    //广告轮播图
    public function slideShow()
    {
        $PluginsSlider = M('plugins_slider')->select();
        //图片控制大小
        /*foreach ($PluginsSlider as $key => $value) {
            $PluginsSlider[$key]['image']=resize($value['image'], 100, 100);
        }*/
        json_data($PluginsSlider);
    }

    //分类列表
    public function category()
    {
        $category_model = D('GoodsCategory');
        $order = 'sort_order desc';
        $limit = '6';
        $category_list = $category_model->categoryList('', '', $order, $limit);
        //数据组装——子分类
        //$list =list_to_tree($category_list);
        json_data($category_list);
    }

    //品牌列表
    public function brand()
    {
        $brand_model = D('GoodsBrand');
        $order = 'sort_order desc';
        $limit = '6';
        $finds = 'id,name,goods_class_id,brand_logo,recommend,letter,sort_order';
        $brand_list = $brand_model->brandList('', $finds, $order, $limit);
        json_data($brand_list);
    }

    //分类，品牌列表
    public function categoryBrand_list()
    {
        //分类列表
        $category_model = D('GoodsCategory');

        //总数、分页
        $c_count = $category_model->categoryCount();

        //接收post转换get
        $_GET['page'] = $_POST['page'];

        //分页数
        $page = $_POST['page'];

        //一页显示的条数
        $num = 10;

        //返回是否有下页数据
        $load = api_page($page, $num, $c_count);

        $finds = 'id,pid,name,category_logo,sort_order';
        $category_list = $category_model->categoryList('', $finds, '', '', $page, $num);
        //数据组装——子分类
        //$list =list_to_tree($category_list);
        $category_id = array();
        foreach ($category_list as $k => $v) {
            $category_id[] = $v['id'];
        }

        //品牌数据列表
        $brand_w = array(
            'goods_class_id' => array('in', $category_id),
        );


        //是否推荐的品牌
        $recommend = $_POST['recommend'];

        if (!empty($recommend)) {
            $brand_w['recommend'] = $recommend;
        }


        $brand_model = D('GoodsBrand');
        $limit = '6';
        $finds = 'id,name,goods_class_id,brand_logo,recommend,letter,sort_order';
        $brand_list = $brand_model->brandList($brand_w, $finds, '', $limit);

        foreach ($category_list as $k => $v) {
            foreach ($brand_list as $vv) {
                if ($vv['goods_class_id'] == $v['id']) {   //判断第一个数组的id等于第二个数组的id
                    $category_list[$k]['brand_list'][] = $vv;    //把要的数据放到一个新的数组里面，作为第二级
                }
            }
        }

        //如果为空，预留下表为空的数据
        foreach ($category_list as $k => $v) {
            if (!isset($v['brand_list'])) {
                $category_list[$k]['brand_list'] = array();
            }
        }

        json_data(array('load' => $load, 'data_list' => $category_list));
    }


    //分类，商品列表
    public function categoryGoods_list()
    {
        //分类列表
        $category_model = D('GoodsCategory');

        //总数、分页
        $c_count = $category_model->categoryCount();

        //接收post转换get
        $_GET['page'] = $_POST['page'];

        //分页数
        $page = $_POST['page'];

        //一页显示的条数
        $num = 10;

        //返回是否有下页数据
        $load = api_page($page, $num, $c_count);

        $finds = 'id,pid,name,category_logo,sort_order';
        $category_list = $category_model->categoryList('', $finds, '', '', $page, $num);

        //数据组装——子分类
        //$list =list_to_tree($category_list);

        //商品列表
        $goods_arr = array();
        foreach ($category_list as $k => $v) {
            $sql = 'SELECT p.goods_id,p.image,p.name,p.price,p.after_discount,p.sort_order,ptc.category_id FROM ' . C('DB_PREFIX') . 'goods p,' .
                C('DB_PREFIX') . 'goods_to_category ptc ' .
                ' WHERE p.goods_id=ptc.goods_id AND p.status=1 AND ptc.category_id=' . $v['id'];
            $sql .= ' order by p.sort_order asc LIMIT 6';
            $list = M()->query($sql);
            if (!empty($list)) {
                $goods_arr[] = $list;
            }
        }

        //组装数组
        foreach ($category_list as $k => $v) {
            foreach ($goods_arr as $kk => $vv) {
                foreach ($vv as $kkk => $vvv) {
                    if ($vvv['category_id'] == $v['id']) {   //判断第一个数组的id等于第二个数组的id
                        $category_list[$k]['goods_list'][] = $vvv;    //把要的数据放到一个新的数组里面，作为第二级
                    }

                }

            }
        }

        //添加月供字段返回
        $arr_arr = array();
        foreach ($category_list as $k => $v) {
            foreach ($v['goods_list'] as $kk => $vv) {
                $v['goods_list'][$kk]['monthly_payment'] = monthly_payment($vv['price'], $vv['after_discount'], $periods = '12');
            }
            $arr_arr[] = $v;
        }

        //如果为空，返回字段一个空数组
        foreach ($arr_arr as $k => $v) {
            if (!array_key_exists('goods_list', $v)) {
                $arr_arr[$k]['goods_list'] = array();
            }
        }

        json_data(array('load' => $load, 'data_list' => $arr_arr));
    }


    //搜索商品列表
    public function searchGoods_list()
    {
        //分类列表
        $goods_model = D('Goods');

        $name = $_POST['name'];
        $g_where = array(
            'name' => array('like', "%" . $name . "%"),
            'status' => 1,
        );
        //总数、分页
        $g_count = $goods_model->goodsCount($g_where);
        //接收post转换get
        $_GET['page'] = $_POST['page'];

        //分页数
        $page = $_POST['page'];

        //一页显示的条数
        $num = 10;

        //返回是否有下页数据
        $load = api_page($page, $num, $g_count);

        $finds = 'goods_id,name,image,price,market_price,after_discount,sort_order';

        //价格排序
        $order = $_POST['order'];
        if (!empty($order)) {
            if ($order == '1') {
                $order = 'price desc';
            } else {
                $order = 'price asc';
            }
        } else {
            $order = 'sort_order asc';
        }

        $goods_list = $goods_model->goodsList($g_where, $finds, $order, '', $page, $num);


        //添加月供字段返回
        foreach ($goods_list as $k => $v) {
            $goods_list[$k]['monthly_payment'] = monthly_payment($v['price'], $v['after_discount'], $periods = '12');
        }

        json_data(array('load' => $load, 'data_list' => $goods_list));
    }


    //点击分类，获取商品列表
    public function getCategoryGoods_list()
    {
        //分页数
        $category_id = $_POST['category_id'];
        if (empty($category_id)) {
            json_error('参数错误');
        }
        //分类列表
        $GoodsToCategory = D('GoodsToCategory');

        $where = array(
            'category_id' => $category_id
        );

        //总数、分页
        $g_count = $GoodsToCategory->goodsCategoryCount($where);

        //接收post转换get
        $_GET['page'] = $_POST['page'];

        //分页数
        $page = $_POST['page'];

        //一页显示的条数
        $num = 10;

        //返回是否有下页数据
        $load = api_page($page, $num, $g_count);

        //查询中间表，goods_id
        $goods_id = $GoodsToCategory->goodsCategoryList($where, '', '', '', $page, $num);

        $goods_arrid = array();
        foreach ($goods_id as $k => $v) {
            $goods_arrid[] = $v['goods_id'];
        }


        //查询商品表信息
        $Goods_model = D('Goods');
        $g_where = array(
            'goods_id' => array('in', $goods_arrid)
        );

        $finds = 'goods_id,name,image,price,market_price,after_discount,sort_order';

        //价格排序
        $order = $_POST['order'];
        if (!empty($order)) {
            if ($order == '1') {
                $order = 'price desc';
            } else {
                $order = 'price asc';
            }
        } else {
            $order = 'sort_order asc';
        }

        $goods_data = $Goods_model->goodsList($g_where, $finds, $order, '', $page, $num);


        //添加月供字段返回
        foreach ($goods_data as $k => $v) {
            $goods_data[$k]['monthly_payment'] = monthly_payment($v['price'], $v['after_discount'], $periods = '12');

        }

        json_data(array('load' => $load, 'data_list' => $goods_data));
    }


    //品牌名称列表
    public function brand_list()
    {
        $brand_model = D('GoodsBrand');

        //总数、分页
        //$b_count = $brand_model->brandCount();

        //接收post转换get
        // $_GET['page'] = $_POST['page'];

        //分页数
        //$page = $_POST['page'];

        //一页显示的条数
        //$num = 100;

        //返回是否有下页数据
        //$load = api_page($page, $num, $b_count);
        $finds = 'id,name,letter';
        //$brand_list = $brand_model->brandList('', $finds, '','',$page, $num );
        //按首字母排序
        $order = 'convert(letter using gb2312) asc';
        $brand_list = $brand_model->brandList('', $finds, $order, '500');

        json_data(array('data_list' => $brand_list));
    }


    //搜索商品列表
    public function goods_list()
    {
        //分类列表
        $goods_model = D('Goods');

        $brand_id = $_POST['brand_id'];
        $g_where = array(
            'brand_id' => $brand_id,
            'status' => 1,
        );
        //总数、分页
        $g_count = $goods_model->goodsCount($g_where);

        //接收post转换get
        $_GET['page'] = $_POST['page'];

        //分页数
        $page = $_POST['page'];

        //一页显示的条数
        $num = 10;

        //返回是否有下页数据
        $load = api_page($page, $num, $g_count);

        $finds = 'goods_id,name,image,price,market_price,after_discount,sort_order';

        //价格排序
        $order = $_POST['order'];
        if (!empty($order)) {
            if ($order == '1') {
                $order = 'price desc';
            } else {
                $order = 'price asc';
            }
        } else {
            $order = 'sort_order asc';
        }

        $goods_list = $goods_model->goodsList($g_where, $finds, $order, '', $page, $num);

        //添加月供字段返回
        foreach ($goods_list as $k => $v) {
            $goods_list[$k]['monthly_payment'] = monthly_payment($v['price'], $v['after_discount'], $periods = '12');
        }

        json_data(array('load' => $load, 'data_list' => $goods_list));
    }


    //商品详情
    public function goods_details()
    {

        $id = I('goods_id');

        //商品主表的数据
        $sql = "select g.goods_id,g.price,g.market_price,g.name,g.after_discount,gd.description from " .
            C('DB_PREFIX') . "goods g," . C('DB_PREFIX') . "goods_description gd where g.goods_id=gd.goods_id and g.goods_id=" . $id;

        $goods_data = M()->query($sql);
        $goods_data = $goods_data[0];

        //商品图片
        $sql = "select image from " . C('DB_PREFIX') . "goods_image where goods_id=" . $id;
        $goods_image = M()->query($sql);

        if (isset($goods_image)) {
            $goods_image_arr = array();
            foreach ($goods_image as $k => $v) {
                //后台商品详情放大镜底图
                $goods_image_arr[] = resize($v['image'], C('goods_thumb_width'), C('goods_thumb_height'));
                //$goods_image[$k]['thumb']=resize($v['image'], C('goods_gallery_thumb_width'), C('goods_gallery_thumb_height'));
            }
        }

        $goods_data['image_list'] = $goods_image_arr;

        //商品选项
        $options = $this->get_goods_options($id);

        //商品点击量
        M()->execute("UPDATE " . C('DB_PREFIX') . "goods SET viewed = (viewed + 1) WHERE goods_id =" . $id);

        $goods_data['options'] = $options;
        json_data(array('data_list' => $goods_data));
    }

    //商品选项
    public function get_goods_options($goods_id)
    {
        $goods_option_data = array();
        $goods_option_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "goods_option po LEFT JOIN "
            . C('DB_PREFIX') . "option o ON po.option_id = o.option_id WHERE po.goods_id =" . (int)$goods_id);
        foreach ($goods_option_query as $goods_option) {
            $goods_option_value_data = array();
            $goods_option_value_query = M()->query("SELECT pov.*,ov.value_name FROM " . C('DB_PREFIX')
                . "goods_option_value pov LEFT JOIN " . C('DB_PREFIX')
                . "option_value ov ON pov.option_value_id=ov.option_value_id"
                . " WHERE pov.goods_option_id = '"
                . (int)$goods_option['goods_option_id'] . "'");

            foreach ($goods_option_value_query as $goods_option_value) {
                $goods_option_value_data[] = array(
                    'goods_option_value_id' => $goods_option_value['goods_option_value_id'],
                    'option_value_id' => $goods_option_value['option_value_id'],
                    'name' => $goods_option_value['value_name'],
                    'image' => isset($goods_option_value['image']) ? $goods_option_value['image'] : '',
                    'price' => $goods_option_value['price'],
                    'price_prefix' => $goods_option_value['price_prefix'],

                );
            }

            $goods_option_data[] = array(
                'goods_option_id' => $goods_option['goods_option_id'],
                'option_id' => $goods_option['option_id'],
                'name' => $goods_option['name'],
                'type' => $goods_option['type'],
                'option_value' => $goods_option_value_data,
                'required' => $goods_option['required']
            );
        }

        return $goods_option_data;
    }

}